import { WorkShopService } from './../../../@core/services/workshop/work-shop.service';
import { Component, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { AddWorkShopComponent } from './add-work-shop/add-work-shop.component';
import { EditWorkShopComponent } from './edit-work-shop/edit-work-shop.component';

@Component({
  selector: 'app-work-shop',
  templateUrl: './work-shop.component.html',
  styleUrls: ['./work-shop.component.scss']
})
export class WorkShopComponent {
  tableData:any[]=[]
  tableheight = 'calc(100vh - 380px)';
  allCheckedStatus = false;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  checkedIds = [];
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any; }[];
  config = {
    id: 'Add-role',
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  filterValue:string = '';
  TreeData:any = [];
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  state: any;
  constructor(private dialogService: DialogService,private SharedMethodService:SharedMethodService,private WorkShopService:WorkShopService){}
  ngOnInit(): void {
    this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tableData=data.items;
      this.pager.total=data.totalCount;
    })
  }
  dataTableOptions = {
    columns: [
      {
        field: 'code',
        header: '车间编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '车间名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '车间主任',
        fieldType: 'text'
      },
      {
        field: 'name',
        header: '线边仓位',
        fieldType: 'text'
      },
      {
        field: 'name',
        header: '状态',
        fieldType: 'text'
      },
      {
        field: 'name',
        header: '创建时间',
        fieldType: 'text'
      },

    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
    {
      field:'name',
      width:'150px'
    },
  ];//表格宽度配置

  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tableData=data.items;
      this.pager.total=data.totalCount;
    })
  }//切换页码
  pageSizeChange($event: any){
    this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tableData=data.items;
      this.pager.total=data.totalCount;
    })
  }//改变每页显示数量
  DeleteRole(rowitem:any,dialogtype?:string){
    const results = this.dialogService.open({
      ...this.config,
      title: '删除车间',
      content: '确认删除该车间？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.WorkShopService.DeleteWorkshop([rowitem.workshopId]).subscribe((data:any)=>{
              results.modalInstance.hide();
              this.showToast('success','成功','车间删除成功')
              this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                this.tableData=data.items;
                this.pager.total=data.totalCount;
              })
            },
            (error) => {
              if(error.status==404){
                this.showToast('error', '失败', '服务器连接失败');
              }else{
                this.showToast('error', '失败', error.error);
              }
              results.modalInstance.hide();
            }
            )
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//删除
  enable(){
    this.WorkShopService.EnableWorkshops(this.checkedIds).subscribe((data:any)=>{
      this.showToast('success','成功','车间启用成功')
      this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tableData=data.items;
        this.pager.total=data.totalCount;
      })
    },(error) => {
      if(error.status==404){
        this.showToast('error', '失败', '服务器连接失败');
      }else{
        this.showToast('error', '失败', error.error);
      }
    })
  }//启用
  disable(){
    this.WorkShopService.DisableWorkshops(this.checkedIds).subscribe((data:any)=>{
      this.showToast('success','成功','车间禁用成功')
      this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tableData=data.items;
        this.pager.total=data.totalCount;
      })
    },(error) => {
      if(error.status==404){
        this.showToast('error', '失败', '服务器连接失败');
      }else{
        this.showToast('error', '失败', error.error);
      }
    })
  }//禁用
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  Creat(dialogtype?:string){
    let data = {
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data:data,
      title: '添加车间',
      content: AddWorkShopComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.workShop.workshopNum&&results.modalContentInstance.workShop.workshopName){
              this.WorkShopService.CreateWorkshop(results.modalContentInstance.workShop).subscribe((data:any)=>{
                results.modalInstance.hide();
                this.showToast('success','成功','车间添加成功')
                this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tableData=data.items;
                  this.pager.total=data.totalCount;
                })
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
                results.modalInstance.hide();
              })
            }
              data.display = true

          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//创建车间
  EditRole(rowItem:any,dialogtype?:string){
    let data = {
      rowItem:rowItem,
      display: false,
    };
    const results = this.dialogService.open({
      ...this.config,
      data:data,
      title: '更新车间',
      content: EditWorkShopComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.workShop.workshopName){
              this.WorkShopService.UpdateWorkshop(results.modalContentInstance.workShop,rowItem.workshopId).subscribe((data:any)=>{
                results.modalInstance.hide();
                this.showToast('success','成功','车间更新成功')
                this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tableData=data.items;
                  this.pager.total=data.totalCount;
                })
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
                results.modalInstance.hide();
              })
            }else{
              data.display = true
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }//更新车间
  filter(){
    this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tableData=data.items;
      this.pager.total=data.totalCount;
    })
  }//搜索
  reset(){
    this.filterValue = '';
    this.WorkShopService.GetWorkshops(this.state,this.filterValue,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tableData=data.items;
      this.pager.total=data.totalCount;
    })
  }//重置
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)

  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable,'workshopId').subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }//获取选中行
  cleared(){
    this.SharedMethodService.cleared(this.checkedIds,this.tableData,this.datatable).subscribe((data:any)=>{
      this.checkedIds=data;

    })
  }//清空选中行
}
